﻿


Public Class Customers
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'check authentication
        If AuthenticationValidator.CheckIfAuthorized(Me, "Customers - View") = False Then
            Me.Response.Redirect("AccessDeniedMsg.aspx")
        End If

        If Me.IsPostBack = False Then
            RefreshCustomers()
            toggleButtonStates()
        End If

    End Sub

    Private Sub toggleButtonStates()

        Add_Button.Enabled = False
        Save_Button.Enabled = False
        Delete_Button.Enabled = False
        If Me.ViewState("SelectedCustomerId") = Guid.Empty Then
            If AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Add") Then Add_Button.Enabled = True
        Else
            If AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Add") Then Add_Button.Enabled = True
            If AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Edit") Then Save_Button.Enabled = True
            If AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Delete") Then Delete_Button.Enabled = True
        End If
        If (AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Add") = False) _
            OrElse (AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Edit") = False) _
            OrElse (AuthenticationValidator.CheckIfAuthorized(Me, "Customers - Delete") = False) Then
            info_Label.Text = "Some functionality of this screen, has been disabled, because you don't have sufficient rights. " & _
                "<br/>If you feel that this is an error, please contact the administrator of this system."
        Else
            If info_Label.Text = "Some functionality of this screen, has been disabled, because you don't have sufficient rights. " & _
                "<br/>If you feel that this is an error, please contact the administrator of this system." Then
                info_Label.Text = ""
            End If
        End If
    End Sub

    Private Sub RefreshCustomers()

        Dim d As New DbHelper
        Customers_ListBox.DataSource = d.GetCustomersList
        Customers_ListBox.DataTextField = "Text"
        Customers_ListBox.DataValueField = "CustomerId"
        Customers_ListBox.DataBind()

    End Sub

    <System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> _
    Public Shared Function GetCountryCompletionList(ByVal prefixText As String, _
                                             ByVal count As Integer, _
                                             ByVal contextKey As String) As String()
        'get countries
        Dim db As New DbHelper
        Dim countries As Collections.ObjectModel.Collection(Of CountryType) = db.GetCountriesList
        'Threading.Thread.Sleep(2000)
        Return (From c As CountryType In countries
                  Where c.Name.ToLower.Contains(prefixText.ToLower)
                  Order By c.Name
                  Select c.Name).Take(count).ToArray
    End Function

    Private Sub Customers_ListBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles Customers_ListBox.SelectedIndexChanged
        SelectCustomer(Customers_ListBox.SelectedItem)
        toggleButtonStates()
        info_Label.Text = ""
    End Sub

    Private Sub SelectCustomer(item As ListItem)

        Dim cid As Guid
        If (item IsNot Nothing) _
            AndAlso (item.Value <> "") Then
            cid = New Guid(item.Value)
        Else
            cid = Nothing
        End If
        If cid = Guid.Empty Then
            Me.ViewState("SelectedCustomerId") = Nothing
            RefreshCustomerProperties()
        Else
            Me.ViewState("SelectedCustomerId") = cid
            RefreshCustomerProperties()
        End If

    End Sub

    Private Sub RefreshCustomerProperties()

        If (Me.ViewState("SelectedCustomerId") IsNot Nothing) _
            AndAlso (CType(Me.ViewState("SelectedCustomerId"), Guid) <> Guid.Empty) Then
            Dim d As New DbHelper
            Dim cid As Guid = Me.ViewState("SelectedCustomerId")
            Dim customer As CustomerType = d.GetCustomerById(cid)
            IdType_DropDownList.SelectedValue = customer.IdType
            IdNumber_TextBox.Text = customer.IdNumber
            Name_TextBox.Text = customer.Name
            Country_TextBox.Text = customer.Country
            Telephone_TextBox.Text = customer.Telephone
        Else
            IdType_DropDownList.SelectedValue = ""
            IdNumber_TextBox.Text = ""
            Name_TextBox.Text = ""
            Country_TextBox.Text = ""
            Telephone_TextBox.Text = ""
        End If

    End Sub

    Protected Sub Add_Button_Click(sender As Object, e As EventArgs) Handles Add_Button.Click

        Dim db As New DbHelper
        Dim customer As New CustomerType
        customer.CustomerId = Guid.NewGuid
        customer.IdType = IdType_DropDownList.SelectedValue
        customer.IdNumber = IdNumber_TextBox.Text
        customer.Name = Name_TextBox.Text
        customer.Country = Country_TextBox.Text
        customer.Telephone = Telephone_TextBox.Text

        Try
            db.AddCustomer(customer)
            RefreshCustomers()
            info_Label.Text = ""
        Catch ex As SqlClient.SqlException
            If ex.Message.Contains("unique index") Then
                info_Label.Text = "Customer Id already exists in the system, cannot insert duplicate customer Id's"
            Else
                info_Label.Text = ex.Message
            End If
        Catch ex As Exception
            info_Label.Text = ex.Message
        End Try

    End Sub

    Protected Sub Save_Button_Click(sender As Object, e As EventArgs) Handles Save_Button.Click

        Dim db As New DbHelper
        Dim cid As Guid = Me.ViewState("SelectedCustomerId")
        Dim customer As CustomerType = db.GetCustomerById(cid)
        customer.IdType = IdType_DropDownList.SelectedValue
        customer.IdNumber = IdNumber_TextBox.Text
        customer.Name = Name_TextBox.Text
        customer.Country = Country_TextBox.Text
        customer.Telephone = Telephone_TextBox.Text
        Try
            db.UpdateCustomer(customer)
            RefreshCustomers()
            info_Label.Text = ""
        Catch ex As SqlClient.SqlException
            If ex.Message.Contains("unique index") Then
                info_Label.Text = "Customer Id already exists in the system, cannot insert duplicate customer Id's"
            Else
                info_Label.Text = ex.Message
            End If
        Catch ex As Exception
            info_Label.Text = ex.Message
        End Try

    End Sub

    Protected Sub Delete_Button_Click(sender As Object, e As EventArgs) Handles Delete_Button.Click

        Dim db As New DbHelper
        Dim cid As Guid = Me.ViewState("SelectedCustomerId")
        Dim customer As CustomerType = db.GetCustomerById(cid)
        Try
            db.DeleteCustomer(customer)
            RefreshCustomers()
            Me.ViewState("SelectedCustomerId") = Nothing
            RefreshCustomerProperties()
            info_Label.Text = ""
        Catch ex As SqlClient.SqlException
            If ex.ErrorCode = -2146232060 Then
                info_Label.Text = "Customer Id already exists in the system, cannot insert duplicate customer Id's"
            End If
        Catch ex As Exception
            info_Label.Text = ex.Message
        End Try

    End Sub


End Class